package tcdx.uap.service.Listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson2.JSON;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tcdx.uap.common.utils.Lutils;
import tcdx.uap.service.BaseDBService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 直接用map接收数据
 *
 */

public class NoModleDataListener extends AnalysisEventListener<Map<Integer, String>>{


    private BaseDBService baseDBService;
    private String table_name;
    private String create_staff_nm;
    private int create_staff_no;
    private int dept_id;
    private List<Map> columns_list;



    
    private static final Logger LOGGER = LoggerFactory.getLogger(NoModleDataListener.class);
    /**
     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 3000;
    List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>();



    public NoModleDataListener(String table_name, String create_staff_nm,List<Map> columns_list,
                               int create_staff_no, int dept_id,  BaseDBService baseDBService) {
        this.baseDBService = baseDBService;
        this.create_staff_nm = create_staff_nm;
        this.create_staff_no = create_staff_no;
        this.table_name = table_name;
        this.dept_id = dept_id;
        this.columns_list = columns_list;
    }


    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
        list.add(data);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        LOGGER.info("所有数据解析完成！");

    }

    /**
     * 加上存储数据库
     */
    private void saveData(){
        List<Map> insertMapList = new ArrayList<>();
        String nowTime = Lutils.getTime();
        Map<Integer, String> colmap = list.get(0);
        Map<Integer, String> coltype = list.get(1);

        for (int i = 2; i < list.size(); i++) {
            Map map = list.get(i);
            Map insertMap = new HashMap();
            for (int j = 0; j < colmap.size(); j++) {
                String field = colmap.get(j);
                String coltype1 = coltype.get(j);
                Object o = map.get(j);
                if(coltype1.equals("文本")){
                    insertMap.put(field,o);
                }else if(coltype1.equals("数字")){
                    insertMap.put(field,Integer.parseInt(o.toString()));
                }else if(coltype1.equals("日期")){
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
                    insertMap.put(field,o);
                }
            }
            insertMap.put("complete_user_",create_staff_nm);
            insertMap.put("complete_group_",dept_id);
            insertMap.put("complete_time_",new Date());

            insertMapList.add(insertMap);
        }
        baseDBService.insertByBatch(table_name,insertMapList);
        baseDBService.insertByBatch(table_name+"_log",insertMapList);
    }

    @Override
    public void invokeHeadMap(Map headMap, AnalysisContext context) {

    }
}